﻿<%@ Page Title="Promotion Engine - Sales Person" Language="C#" MasterPageFile="~/Site.Master"
    AutoEventWireup="true" CodeBehind="SalesPerson.aspx.cs" Inherits="PromotionEngine.SalesPerson" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <script type="text/javascript">
        var oSalesPerson = {};
        var isInsert = false;
        var gird;
        var girdDetail;
        var wnd, detailsTemplate;
        var jsonSalesPerson = {};
        function GetAllSalesPerson() {
            $.ajax({
                type: "GET",
                url: "SalesPerson.aspx/GetSalesPersonForManager",
                data: '',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    jsonSalesPerson = $.parseJSON(data.d)
                },
                failure: function (response) {
                    alert(response.d);
                },
                error: function (xhr, status, err) {
                    var err = eval("(" + xhr.responseText + ")");
                    alert(err.Message);
                }
            });
        }
        $(document).ready(function () {
            GetAllSalesPerson();
            //Load Grid
            grid = $("#grid").kendoGrid({
                dataSource: {
                    transport: {
                        read: {
                            type: "GET",
                            url: "SalesPerson.aspx/GetSalesPersonList",
                            contentType: "application/json; charset=utf-8",
                            dataType: "json"
                        },
                        destroy: {
                            type: "GET",
                            url: "SalesPerson.aspx/DeleteSalesPerson",
                            contentType: "application/json; charset=utf-8",
                            dataType: "json"
                        },
                        parameterMap: function (data, operation) {
                            if (operation === "destroy") {
                                return { salesPersonID: data.SalesPersonID };
                            }
                        },
                        serverPaging: true,
                        serverSorting: true,
                        serverFiltering: true
                    },
                    type: "odata", //Important!!!!
                    schema: {
                        data: function (a) {
                            if (a.d != null) {
                                var data = a.d.Data;
                                return data;
                            }
                        },
                        total: "d.Count",
                        model: {
                            id: "SalesPersonID",
                            fields: {
                                SalesPersonID: { type: "string", validation: { required: true} },
                                SalesPersonCode: { type: "string", validation: { required: true} },
                                SalesPersonName: { type: "string", validation: { required: true} },
                                CompanyCode: { type: "string" },
                                CompanyName: { type: "string" },
                                ManagerID: { type: "string" },
                                ManagerCode: { type: "string" },
                                ManagerName: { type: "string" }
                            }
                        }
                    }
                },
                sortable: true,
                pageable: {
                    pageSize: 20,
                    refresh: true
                },
                filterable: true,
                reorderable: true,
                resizable: true,
                selectable: "row",
                height: 450,
                toolbar: [{ name: 'create', text: 'Add New Sales Person' }, { name: 'excel'}],
                excel: {
                    fileName: "Sales Person List.xlsx",
                    filterable: true,
                    allPages: true
                },
                batch: true,
                columns: [{ field: "SalesPersonID", title: "SalesPersonID", width: 100, hidden: true },
                            { field: "SalesPersonCode", title: "Code", width: 200 },
                            { field: "SalesPersonName", title: "Name" },
                            { field: "ManagerCode", title: "Manager Code", hidden: true },
                            { field: "ManagerID", title: "Manager ID", hidden: true },
                            { field: "ManagerName", title: "Manager ", width: 300 },
                            { field: "CompanyCode", title: "CompanyCode ", hidden: true },
                            { field: "CompanyName", title: "Company " },
                            { command: ["edit", "destroy"], title: "Action", width: "270px" }
                            ],
                editable: {
                    mode: "popup",
                    template: $("#template").html(),
                    confirmation: "Are you sure you want to remove this sales person?"
                },
                edit: function (e) {
                    $('#dllSalesPerson').kendoDropDownList({
                        autoBind: false,
                        filter: "contains",
                        optionLabel: "[Select Manager]",
                        suggest: true,
                        dataTextField: "SalesPersonName",
                        dataValueField: "SalesPersonID",
                        dataSource: {
                            data: jsonSalesPerson
                        }

                    });
                    var editWindow = e.container.data("kendoWindow");
                    if (e.model.isNew()) {
                        $('[name="SalesPersonID"]').attr("readonly", true);
                        e.container.data("kendoWindow").title('Add New Sales Person');
                        $("a.k-grid-update")[0].innerHTML = "<span class='k-icon k-update'></span>Save";
                        isInsert = true;
                    }
                    else {
                        e.container.data("kendoWindow").title('Edit Sales Person');
                        isInsert = false;
                    }
                },
                remove: function (e) {
                },
                save: function (e) {
                    var listTextBox = $("input:text");
                    for (var i = 0; i < listTextBox.length; i++) {
                        var name = listTextBox[i].name;
                        if (name.length > 0) {
                            oSalesPerson[name] = $("[name=" + name + "]").val();
                        }
                    }
                    oSalesPerson.ManagerID = $("#dllSalesPerson").data("kendoDropDownList").value();
                    //Update or Insert Company
                    $.ajax({
                        type: "POST",
                        url: "SalesPerson.aspx/UpdateSalesPerson",
                        data: JSON.stringify({
                            oSalesPerson: JSON.stringify(oSalesPerson),
                            isInsert: isInsert
                        }),
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (data) {
                            //Reload Data 
                            grid.dataSource.read();
                            GetAllSalesPerson();
                        },
                        error: function (xhr, status, err) {
                            var err = eval("(" + xhr.responseText + ")");
                            alert(err.Message);
                        }
                    });
                    this.refresh();
                }
            }).data("kendoGrid");
        });
    </script>
    <script type="text/x-kendo-template" id="template">  
       <div class="k-edit-label"  style="display: none;">
        <label for="SalesPersonID">
            ID</label></div>
    <div class="k-edit-field" data-container-for="SalesPersonID"  style="display: none;">
        <input name="SalesPersonID" class="k-input k-textbox" required="required" type="text"
            data-bind="value:SalesPersonID"></div>
    <div class="k-edit-label">
        <label for="SalesPersonCode">
            Code</label></div>
    <div class="k-edit-field" data-container-for="SalesPersonCode">
        <input name="SalesPersonCode" class="k-input k-textbox" required="required" type="text"
            data-bind="value:SalesPersonCode"></div>
    <div class="k-edit-label">
        <label for="SalesPersonName">
            Name</label></div>
    <div class="k-edit-field" data-container-for="SalesPersonName">
        <input name="SalesPersonName" class="k-input k-textbox" required="required" type="text"
            data-bind="value:SalesPersonName"></div>
    <div class="k-edit-label">
        <label for="ManagerID">
            Manager</label></div>
    <div data-container-for="ManagerID" class="k-edit-field">
        <input id="dllSalesPerson" name="ManagerID" data-bind="value:ManagerID" />
    </div>
    <div class="k-edit-label" style="display: none;">
        <label for="CompanyCode">
            CompanyCode
        </label>
    </div>
    <div class="k-edit-field" data-container-for="CompanyCode" style="display: none;">
        <input name="CompanyCode" class="k-input k-textbox" type="text" data-bind="value:CompanyCode"></div>
    </script>
    <div style="position: absolute; z-index: 0; margin-right: 8px;">
        <div id="grid">
        </div>
    </div>
   
</asp:Content>
